package com.mjh.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mjh.pojo.WChxhGzjlDt2;
import com.mjh.service.IWChxhGzjlDt2Service;
import com.mjh.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;

/**
 *
 * 工作经历 前端控制器
 *
 *
 * @author MJH
 * @since 2021-12-03
 */
@RestController
@RequestMapping("/work")
public class WChxhGzjlDt2Controller {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private IWChxhGzjlDt2Service iwChxhGzjlDt2Service;
    /**
     * 工作经历添加
     * @param dt2
     */
    @PostMapping("/workadd")
    public  void workadd(@RequestBody WChxhGzjlDt2 dt2){
        Integer userId = (Integer) JwtUtil.getPayload(request, "userId");
        dt2.setCreateUserId(userId);
        iwChxhGzjlDt2Service.save(dt2);
    }

    /**
     * 工作经历批量或单一删除
     * @param workid
     */
    @PostMapping("/workdle")
    public void workdle(@RequestBody Integer [] workid){
       iwChxhGzjlDt2Service.removeByIds(Arrays.asList(workid));
    }

    /**
     * 工作经历分页查询
     * @param currentPage 当前页码
     * @param pageSize 每页数量
     * @return
     */
    @GetMapping("/workslt")
    public ResponseEntity<Object> workslt(@RequestParam(defaultValue = "1") Integer currentPage,
                                          @RequestParam(defaultValue = "10")  Integer pageSize){
        Page<WChxhGzjlDt2> page = new Page<>(currentPage,pageSize);
        Integer userId = (Integer)JwtUtil.getPayload(request, "userId");
        Page<WChxhGzjlDt2> wtzxPage = iwChxhGzjlDt2Service.workslt(page,userId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",wtzxPage.getTotal());
        map.put("currentPage",wtzxPage.getCurrent());
        map.put("list",wtzxPage.getRecords());
        return ResponseEntity.ok(map);
    }
}
